﻿using LdtDataEngin.ModuleDetail.PersonnelAnalysis;
using LdtJudge.DataEngine.Model.DataModel.DataParseStorage;
using LdtJudge.DataEngine.Model.DataModel.ElementNode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LdtDataEngin.ModuleDetail.CaseExtract.Evidence
{
    public class EvidenceClass: EvidenceBaseClass
    {
        public EvidenceClass()
        {
            XmlPersonnel = new Dictionary<string, string>();
            XmlPersonnel.Add(@"刑事案件信息要素中心\审判\一审\庭审阶段\庭审详情\法庭调查\法庭调查详情\证据信息\质证意见详情\质证人", "CE_NAME");
            XmlPersonnel.Add(@"刑事案件信息要素中心\审判\一审\庭审阶段\庭审详情\法庭调查\法庭调查详情\证据信息\质证意见详情\诉讼地位", "ETR_SuitDegreeID");
            XmlPersonnel.Add(@"民事案件信息要素中心\审判\一审\庭审阶段\庭审详情\法庭调查\法庭调查详情\证据\质证意见详情\质证人", "CE_NAME");
            XmlPersonnel.Add(@"民事案件信息要素中心\审判\一审\庭审阶段\庭审详情\法庭调查\法庭调查详情\证据\质证意见详情\诉讼地位", "ETR_SuitDegreeID");
        }
        /// <summary>
        /// 需要转换的节点信息
        /// </summary>
        public Dictionary<string, string> XmlPersonnel { get; set; }

        /// <summary>
        /// 获取人员对应信息
        /// </summary>
        public void EvidencePersonnel()
        {
            foreach (var dicvalue in XmlPersonnel)
            {
                string xmlpath = dicvalue.Key;
                ElementNodeModel elementNodePersonnel = dataContext.DataLayerAns.ElementNodeModelsList.FirstOrDefault(a => a.Path == xmlpath);
                if (elementNodePersonnel != null)
                {
                    string paersonnel = elementNodePersonnel.Value;
                    An_anjianshiti anjianshiti = dataContext.DataLayerAns.Anjianshitis.FirstOrDefault(a => a.Anjianshitijueses.FirstOrDefault(c => c.Fayanrenmingcheng == paersonnel) != null);
                    if (anjianshiti == null)
                    {
                        PersonnelConversion personnelConversion = new PersonnelConversion() { spokenamelist = dataContext.DataLayerAns.ElementSpokeList };
                        paersonnel = personnelConversion.ReplaceShortNameToOriginalText(dataContext.DataLayerAns.ElementSpokeList, paersonnel, dataContext.DataLayerAns.CaseType);
                        paersonnel = personnelConversion.GetMatchSpokeManByRoleName(dataContext.DataLayerAns.ElementSpokeList, paersonnel)[0].ES_FullName;
                        anjianshiti = dataContext.DataLayerAns.Anjianshitis.FirstOrDefault(a => a.Mingcheng == paersonnel);
                    }

                    if (anjianshiti != null)
                    {
                        System.Reflection.PropertyInfo[] properties = anjianshiti.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                        System.Reflection.PropertyInfo propertyInfo = properties.FirstOrDefault(a => a.Name.ToLower() == dicvalue.Value.ToLower());
                        if (propertyInfo != null)
                        {
                            string value = propertyInfo.GetValue(anjianshiti, null).ToString();
                            elementNodePersonnel.Value = value;
                            elementNodePersonnel.OriginalTextList[0] = value;
                        }
                        else
                        {
                            System.Reflection.PropertyInfo[] propertiessuit = anjianshiti.Anjianshitijueses.FirstOrDefault(a => a != null).GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                            System.Reflection.PropertyInfo propertyInfosuit = propertiessuit.FirstOrDefault(a => a.Name.ToLower() == dicvalue.Value.ToLower());
                            if (propertyInfosuit != null)
                            {
                                string value = propertyInfosuit.GetValue(anjianshiti.Anjianshitijueses.FirstOrDefault(a => a != null), null).ToString();
                                elementNodePersonnel.Value = value;
                                elementNodePersonnel.OriginalTextList[0] = value;
                            }
                        }

                    }

                }
            }
        }

    }
}
